package com.laomou.poker;

import java.util.ArrayList;
import java.util.List;

public class PokerGroup {
    private PokerGroupType mType;
    private int mValue = -1;
    private ArrayList<Poker> mPokers = new ArrayList<Poker>();

    public PokerGroupType type() {
        return mType;
    }

    public void setType(PokerGroupType type) {
        this.mType = type;
    }

    public void addPokerGroup(PokerGroup poker) {
        for (Poker p : poker.get()) {
            mPokers.add(p);
        }
    }

    public void addPokerNum(PokerNum pn) {
        mPokers.add(new Poker(pn));
    }

    public void addPoker(Poker poker) {
        mPokers.add(poker);
    }

    public void addPokers(List<Poker> list) {
        mPokers.clear();
        for (Poker p : list) {
            addPoker(p);
        }
    }

    public void removePokerGroup(PokerGroup pg) {
        for (Poker p : pg.get()) {
            mPokers.remove(p);
        }
    }

    public void removePoker(Poker poker) {
        mPokers.remove(poker);
    }

    public Poker get(int i) {
        return mPokers.get(i);
    }

    public void set(int i, Poker p) {
        mPokers.set(i, p);
    }

    public int size() {
        return mPokers.size();
    }

    public List<Poker> get() {
        return mPokers;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + mPokers.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PokerGroup other = (PokerGroup) obj;
        if (mPokers == null) {
            if (other.mPokers != null)
                return false;
        } else if (!mPokers.equals(other.mPokers))
            return false;
        return true;
    }

    @Override
    public String toString() {
        String str = "";
        if (mType != null) {
            str += "(" + mType.name() + ")";
        }
        if (mValue != -1) {
            str += "(" + mValue + ")";
        } else {
            str += "(" + value() + ")";
        }
        for (Poker p : mPokers) {
            str += p;
        }
        return str;
    }

    public void clear() {
        mPokers.clear();
    }

    public int value() {
        int v = 0;
        for (Poker p : mPokers) {
            v += p.value();
        }
        return v;
    }

    public int getValue() {
        int ret = value();
        if (mValue != -1) {
            ret = mValue;
        }
        return ret;
    }

    public void setValue(int value) {
        mValue = value;
    }

    public boolean contains(PokerNum pn) {
        for (Poker p : mPokers) {
            if (p.getPokerNum() == pn) {
                return true;
            }
        }
        return false;
    }

    public boolean contains(int value) {
        for (Poker p : mPokers) {
            if (p.value() == value) {
                return true;
            }
        }
        return false;
    }
}
